package com.chesnutconsulting.otbasket.image.finder.artesia;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import com.artesia.common.exception.BaseTeamsException;
import com.artesia.entity.TeamsIdentifier;
import com.artesia.security.SecuritySession;
import com.artesia.security.session.services.AuthenticationServices;

public class OtBasketConfiguration implements InitializingBean {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(OtBasketConfiguration.class);

	// @NotEmpty
	// @JsonProperty
	private String watermarkResourceLocation;

	// @NotEmpty
	// @JsonProperty
	private String artesiaServerUrl;

	// @NotEmpty
	// @JsonProperty
	private String artesiaUser;

	// @NotEmpty
	// @JsonProperty
	private String artesiaPwd;

	private String securitySessionUser;

	private String securitySessionPass;
	
	private String tmpDir;

	private String outputPath;
	
	private String watermarkFilePath;

	private String maxAssets;
	
	private String teamsHome;

	/**
	 * Base uri for image streams
	 */
	private String baseImageFetchUrl;

	private SecuritySession session;

	private List<TeamsIdentifier> scalarFieldList;

	public String getWatermarkResourceLocation() {
		return watermarkResourceLocation;
	}

	public String getArtesiaServerUrl() {
		return artesiaServerUrl;
	}

	public String getArtesiaUser() {
		return artesiaUser;
	}

	public String getArtesiaPwd() {
		return artesiaPwd;
	}

	public void setWatermarkResourceLocation(String watermarkResourceLocation) {
		this.watermarkResourceLocation = watermarkResourceLocation;
	}

	public void setArtesiaServerUrl(String artesiaServerUrl) {
		this.artesiaServerUrl = artesiaServerUrl;
	}

	public void setArtesiaUser(String artesiaUser) {
		this.artesiaUser = artesiaUser;
	}

	public void setArtesiaPwd(String artesiaPwd) {
		this.artesiaPwd = artesiaPwd;
	}

	public List<TeamsIdentifier> getScalarFieldList() {
		return scalarFieldList;
	}

	public void setScalarFieldList(List<TeamsIdentifier> scalarFieldList) {
		this.scalarFieldList = scalarFieldList;
	}

	public String getSecuritySessionUser() {
		return securitySessionUser;
	}

	public void setSecuritySessionUser(String securitySessionUser) {
		this.securitySessionUser = securitySessionUser;
	}

	public String getSecuritySessionPass() {
		return securitySessionPass;
	}

	public void setSecuritySessionPass(String securitySessionPass) {
		this.securitySessionPass = securitySessionPass;
	}

	public String getBaseImageFetchUrl() {
		return baseImageFetchUrl;
	}

	public void setBaseImageFetchUrl(String baseImageFetchUrl) {
		this.baseImageFetchUrl = baseImageFetchUrl;
	}

	public SecuritySession getSession() throws Exception {
		// Check if session is alive.
		// Optionally configure timed refresh where a logout and login are
		// executed
		try {
			System.setProperty("TEAMS_HOME", this.getTeamsHome());
			AuthenticationServices.getInstance().authenticateUserSession(
					this.session);
		} catch (Exception e) {
			try {
				this.session = AuthenticationServices.getInstance().login(
						getArtesiaUser(), getArtesiaPwd());
			} catch (BaseTeamsException x) {
				// TODO: Fatal - email administrator
				throw x;
			}
		}
		return session;
	}

	public void setSession(SecuritySession session) {
		this.session = session;
	}

	public void afterPropertiesSet() throws Exception {
		try {
			LOGGER.info("Acquiring Artesia session");
			System.setProperty("TEAMS_HOME", this.getTeamsHome());
			this.session = AuthenticationServices.getInstance().login(
					getArtesiaUser(), getArtesiaPwd());
		} catch (BaseTeamsException e) {
			e.printStackTrace();
		}
	}

	public String getTmpDir() {
		return tmpDir;
	}

	public void setTmpDir(String tmpDir) {
		this.tmpDir = tmpDir;
	}

	public String getOutputPath() {
		return outputPath;
	}

	public void setOutputPath(String outputPath) {
		this.outputPath = outputPath;
	}

	public String getWatermarkFilePath() {
		return watermarkFilePath;
	}

	public void setWatermarkFilePath(String watermarkFilePath) {
		this.watermarkFilePath = watermarkFilePath;
	}

	public String getMaxAssets() {
		return maxAssets;
	}

	public void setMaxAssets(String maxAssets) {
		this.maxAssets = maxAssets;
	}

	public String getTeamsHome() {
		return teamsHome;
	}

	public void setTeamsHome(String teamsHome) {
		this.teamsHome = teamsHome;
	}
	
}
